<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up()
    {
        // 确保 code_activities 表存在
        if (!Schema::hasTable('code_activities')) {
            Schema::create('code_activities', function (Blueprint $table) {
                $table->id();
                $table->string('name')->comment('活动名称');
                $table->decimal('value', 10, 2)->comment('兑换码价值');
                $table->integer('total_codes')->comment('生成兑换码总数');
                $table->foreignId('product_id')->nullable()->constrained()->onDelete('set null')->comment('可兑换商品ID');
                $table->timestamp('end_time')->nullable()->comment('结束兑换时间');
                $table->boolean('is_active')->default(true)->comment('是否可以兑换');
                $table->timestamps();
            });
        }

        Schema::create('verification_codes', function (Blueprint $table) {
            $table->id();
            $table->string('code')->unique()->comment('验证码');
            $table->string('card_number')->nullable()->comment('兑换码卡号');
            $table->foreignId('wechat_user_id')->nullable()->constrained()->onDelete('set null');
            $table->boolean('is_used')->default(false)->comment('是否已使用');
            $table->timestamp('used_at')->nullable()->comment('使用时间');
            $table->timestamp('expired_at')->nullable()->comment('过期时间');
            $table->foreignId('code_activity_id')->constrained('code_activities')->comment('所属活动ID');
            $table->decimal('value', 10, 2)->comment('兑换码价值');
            $table->timestamps();

            $table->index('card_number');
        });
    }

    public function down()
    {
        Schema::dropIfExists('verification_codes');
        Schema::dropIfExists('code_activities');
    }
}; 